//
//  HFNewFeatureCollectionViewController.swift
//  sinaMicroBlog
//
//  Created by mac on 15/11/15.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private let imageCount = 4

class HFNewFeatureCollectionViewController: UICollectionViewController {

    //重载实例化方法
    init() {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout: layout)
        
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Register cell classes
        self.collectionView!.registerClass(HFNewFeatureCollectionViewcell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! HFNewFeatureCollectionViewcell
    
        // Configure the cell
        cell.index = indexPath.item
        return cell
    }
    //判断是否需要动画效果
    //此处有一bug
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / screenW)
        print(page)
        let cell = collectionView?.visibleCells().last as? HFNewFeatureCollectionViewcell
        if page == imageCount - 1 {
            if  (cell != nil) {
                cell!.startAnimation()
            }
        }else {
            cell!.startButton.hidden = true
        }
    }
}

class HFNewFeatureCollectionViewcell: UICollectionViewCell {
    
    //下一步,设置index属性,并加载图片
    var index: Int = 0 {
        didSet {
            iconImage.image = UIImage(named: "new_feature_\(index + 1)")
        }
    }
    //重载实例化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func startAnimation() {
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("ok")
        }
    }
    //实现点击方法
    @objc private func start() {
        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewController, object: nil)
    }
    
    
    //设置UI
    private func setUpUI() {
        addSubview(iconImage)
        addSubview(startButton)
        
        iconImage.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        startButton.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
            make.centerX.equalTo(contentView.snp_centerX)
        }
        
        startButton.hidden = true
        //添加点击事件
        startButton.addTarget(self, action: "start", forControlEvents: .TouchUpInside)
        
    }
    //懒加载控件
    private lazy var iconImage: UIImageView = UIImageView()
    private lazy var startButton: UIButton = UIButton(title: "开始体验", backImage: "new_feature_finish_button", titleColor: nil)
    
}




